package gttp.nspa.client.adm.ui;

import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.widget.client.TextButton;
import com.sencha.gxt.widget.core.client.ContentPanel;
import com.sencha.gxt.widget.core.client.FramedPanel;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData;
import com.sencha.gxt.widget.core.client.form.FieldLabel;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.IntegerPropertyEditor;
import com.sencha.gxt.widget.core.client.form.SpinnerField;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;

public class ProductForm implements IsWidget {

    protected String                  caption;
    protected int                     formWidth;
    protected int                     labelWidth;

    protected TextField               tfName;
    protected SpinnerField<Integer>   sfPrice;
    protected TextArea                taDesc;

    protected TextButton              btnSave;
    protected TextButton              btnCancel;

    protected VerticalLayoutContainer con;
    protected ContentPanel            pane;

    public ProductForm(String caption, int formWidth, int labelWidth) {
        this.caption = caption;
        this.formWidth = formWidth;
        this.labelWidth = labelWidth;
    }

    public Widget asWidget() {
        if (pane == null) {
            pane = createContentPane(caption, formWidth);
            con = new VerticalLayoutContainer();
            pane.add(con);
            createFormFields(con);
            createFormCommands(pane);
        }
        return pane;
    }

    protected ContentPanel createContentPane(String caption, int width) {
        FramedPanel pane = new FramedPanel();
        pane.setHeadingText(caption);
        pane.setWidth(width);
        pane.setBodyStyle("background: none; padding: 5px");
        return pane;
    }

    protected void createFormFields(VerticalLayoutContainer con) {
        FieldLabel fl;

        tfName = new TextField();
        fl = new FieldLabel(tfName, "Tên");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        sfPrice = new SpinnerField<Integer>(new IntegerPropertyEditor());
        sfPrice.getPropertyEditor().setFormat(NumberFormat.getFormat(",### 'đ'ồ'n'g"));
        sfPrice.setIncrement(new Integer(1000));
        sfPrice.setMinValue(new Integer(10000));
        sfPrice.setMaxValue(new Integer(10000000));
        sfPrice.setValue(new Integer(350000));
        sfPrice.setAllowNegative(false);
        fl = new FieldLabel(sfPrice, "Giá");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, -1));

        taDesc = new TextArea();
        fl = new FieldLabel(taDesc, "Mô tả");
        fl.setLabelWidth(labelWidth);
        con.add(fl, new VerticalLayoutData(1, 100));
    }

    protected void createFormCommands(ContentPanel pane) {
        btnSave = new TextButton("Save");
        btnCancel = new TextButton("Cancel");

        pane.addButton(btnSave);
        pane.addButton(btnCancel);
    }
}
